package com.ymatou.userservice.testcase;

import java.util.Date;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.LoginBillsRequestDtoBean;
import com.ymatou.userservice.service.LoginBillsRequestDtoCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.model.YmtUsers;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.BillsActionType;
import com.ymttest.utils.constant.ECode;

/**
 * 修改登入凭据信息 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_LoginBillsRequestDto {
	private static LoginBillsRequestDtoBean lbb;
	private static LoginBillsRequestDtoCall lbc;
	private static ULC201406Wapper userWapper;
	
	String UserName = EnvSetup.getData("BuyUserName");
	String userId = EnvSetup.getData("BuyUserId");
	String pwd = EnvSetup.getData("BuyUserPassword");
	String phone = EnvSetup.getData("BuyUserPhone");

	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("修改登入账户信息");
		userWapper = new ULC201406Wapper();
	}

	@Before
	public void caseUp() {
		lbb = new LoginBillsRequestDtoBean();
		lbc = new LoginBillsRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(lbc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_001(){
		Logger.start(true, "绑定手机");
		try{
			Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			String phone = TestDataManager.getPhoneNum();
			int userId = (Integer)user.get("userId");
			lbb.setMobile(phone);
			lbb.setUserId(userId);
			lbb.setAction(BillsActionType.BINDMOBILE);
			lbb.setLoginPassword("123456");
			lbb.setActionSource("mainsite");
			lbb.setLoginId(user.get("loginId").toString());
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", lbc.getString("Success"), "验证操作成功");
			Logger.verifyEquals("ok", lbc.getString("SuccessMessage"), "验证Msg信息");
			Logger.verifyEquals("Success", lbc.getString("Result"), "验证Resul信息");
			
			ULC201406Bean updatedUser = userWapper.getULCInfoByUid(userId);
			Logger.verifyEquals(phone, updatedUser.getlBills().getMobile(), "验证手机信息");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_002(){
		Logger.start(true, "修改邮箱信息");
		try{
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			String email = TestDataManager.getEmail();
			int userId = (Integer)user.get("userId");
			lbb.setMail(email);
			lbb.setUserId(userId);
			lbb.setAction(BillsActionType.BINDLOGINMAIL);
			lbb.setLoginPassword("123456");
			lbb.setActionSource("mainsite");
			lbb.setLoginId(user.get("loginId").toString());
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", lbc.getString("Success"), "验证操作成功");
			Logger.verifyEquals("ok", lbc.getString("SuccessMessage"), "验证Msg信息");
			Logger.verifyEquals("Success", lbc.getString("Result"), "验证Resul信息");
			
			ULC201406Bean updatedUser = userWapper.getULCInfoByUid(userId);
			Logger.verifyEquals(email.toLowerCase(), updatedUser.getlBills().getEmail(), "验证手机信息");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_003(){
		Logger.start(true, "激活邮箱");
		try{
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			String email = TestDataManager.getEmail();
			int userId = (Integer)user.get("userId");
			lbb.setMail(email);
			lbb.setUserId(userId);
			lbb.setAction(BillsActionType.BINDLOGINMAIL);
			lbb.setLoginPassword("123456");
			lbb.setActionSource("mainsite");
			lbb.setLoginId(user.get("loginId").toString());
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", lbc.getString("Success"), "验证操作成功");
			Logger.verifyEquals("ok", lbc.getString("SuccessMessage"), "验证Msg信息");
			Logger.verifyEquals("Success", lbc.getString("Result"), "验证Resul信息");
			
			ULC201406Bean updatedUser = userWapper.getULCInfoByUid(userId);
			Logger.verifyEquals(email.toLowerCase(), updatedUser.getlBills().getEmail(), "验证邮箱信息");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_004() {
		Logger.start(true,"修改登入账户信息BindLoginMail成功");
		try {
			// {"UserId":1113,"Mail":"zhao@ymatou.com","LoginPassword":"123456","OldLoginPassword":"","OldTradingPassword":"","ActionSource":1,"Action":"BindLoginMail","LoginId":"test007"}
			Logger.comment("前置条件准备");
			String phone = TestDataManager.getPhoneNum();
			String email = TestDataManager.getEmail();
			String ip = TestDataManager.getRandomIp();
			Map UserMap = UserCallService.registerUser(email, phone, ip);
			String AccessToken = (String) UserMap.get("token");
			int userId = (Integer) UserMap.get("userId");
			String UserName = (String) UserMap.get("loginId");
			Date oldtime = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId)).getLastupdatetime();
			lbb.setAction("BindLoginMail");
			lbb.setUserId(Integer.valueOf(userId));
			lbb.setLoginPassword(pwd);
			lbb.setLoginId(UserName);
			lbb.setActionSource("1");
			lbb.setMail(TestDataManager.getEmail());
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
			YmtUsers users = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId));
			Logger.verifyEquals(true, users.getLastupdatetime().after(oldtime),
					"验证YmtUsers Lastupdatetime更新:" + users.getLastupdatetime());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_005() {
		Logger.start(true,"修改登入账户信息ActivateEmail成功");
		try {
			// {"UserId":1113,"Action":"ActivateEmail",}
			Logger.comment("获取uid:" + userId + "用户的Lastupdatetime");
			String phone = TestDataManager.getPhoneNum();
			String email = TestDataManager.getEmail();
			String ip = TestDataManager.getRandomIp();
			Map UserMap = UserCallService.registerUser(email, phone, ip);
			String AccessToken = (String) UserMap.get("token");
			int userId = (Integer) UserMap.get("userId");
			
			
			Date oldtime = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId)).getLastupdatetime();
			lbb.setAction("ActivateEmail");
			lbb.setUserId(Integer.valueOf(userId));
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
			YmtUsers users = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId));
			Logger.verifyEquals(true, users.getLastupdatetime().after(oldtime),
					"验证YmtUsers Lastupdatetime更新:" + users.getLastupdatetime());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_006() {
		Logger.start(true,"修改登入账户信息ModifyLoginId成功");
		try {
			// {"UserId":1113,"ActionSource":0,"Action":"ModifyLoginId","LoginId":"test0007"}
			Logger.comment("获取uid:" + userId + "用户的Lastupdatetime");
			Date oldtime = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId)).getLastupdatetime();
			lbb.setAction("ModifyLoginId");
			lbb.setUserId(Integer.valueOf(userId));
			lbb.setLoginId(UserName + TestDataManager.getRandomNumToHex(10));
			lbb.setActionSource("0");
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
			YmtUsers users = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId));
			Logger.verifyEquals(true, users.getLastupdatetime().after(oldtime),
					"验证YmtUsers Lastupdatetime更新:" + users.getLastupdatetime());
			Logger.comment("还原账号");
			lbb.setAction("ModifyLoginId");
			lbb.setUserId(Integer.valueOf(userId));
			lbb.setLoginId(UserName);
			lbb.setActionSource("0");
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_010() {
		Logger.start(true,"新增加----修改登入账户信息ModifyLoginId成功");
		try {
			// {"UserId":1113,"ActionSource":0,"Action":"ModifyLoginId","LoginId":"test0007"}
			Logger.comment("获取uid:" + userId + "用户的Lastupdatetime");
			//int maxuserid = new YmtUsersWapper().getMaxUserId()+1;
			
			String phone = TestDataManager.getPhoneNum();
			String email = TestDataManager.getEmail();
			String ip = TestDataManager.getRandomIp();
			Map UserMap = UserCallService.registerUser(email, phone, ip);
			String AccessToken = (String) UserMap.get("token");
			int userId = (Integer) UserMap.get("userId");
			String loginId = (String) UserMap.get("loginId");
			
			Date oldtime = new YmtUsersWapper().getYmtUsers(userId).getLastupdatetime();
			
			lbb.setAction("ModifyLoginId");
			lbb.setUserId(userId);
			lbb.setLoginId(loginId+1);
			lbb.setActionSource("0");
			lbc.setData(lbb);
			lbc.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
			YmtUsers users = new YmtUsersWapper().getYmtUsers(userId);
			Logger.verifyEquals(true, users.getLastupdatetime().after(oldtime),
					"验证YmtUsers Lastupdatetime更新:" + users.getLastupdatetime());
			Logger.comment("还原账号");
			lbb.setAction("ModifyLoginId");
			lbb.setUserId(userId);
			lbb.setLoginId(loginId);
			lbb.setActionSource("0");
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_007() {
		Logger.start(true,"修改登入账户信息BindMobile成功");
		try {
			lbb.setAction("BindMobile");
			lbb.setUserId(1113);
			lbb.setLoginId("xx"); 
			lbb.setActionSource("0");
			lbb.setMobile("13888881234"); 
			lbb.setLoginPassword("_NULLYMATOU_");
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_008() {
		Logger.start(true,"修改登入账户信息ModifyLoginId成功,用户超过20位");
		try {
			// {"UserId":1113,"ActionSource":0,"Action":"ModifyLoginId","LoginId":"test0007"}
			Logger.comment("获取uid:" + userId + "用户的Lastupdatetime");
			Date oldtime = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId)).getLastupdatetime();
			lbb.setAction("ModifyLoginId");
			lbb.setUserId(Integer.valueOf(userId));
			lbb.setLoginId("autotest_"+TestDataManager.getRandomNumToHex(20));
			lbb.setActionSource("0");
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
			YmtUsers users = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId));
			Logger.verifyEquals(true, users.getLastupdatetime().after(oldtime),
					"验证YmtUsers Lastupdatetime更新:" + users.getLastupdatetime());
			Logger.comment("还原账号");
			lbb.setAction("ModifyLoginId");
			lbb.setUserId(Integer.valueOf(userId));
			lbb.setLoginId(UserName);
			lbb.setActionSource("0");
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_009() {
		Logger.start(false,"修改登入账户信息ModifyLoginId失败用户超过100位");
		try {
			// {"UserId":1113,"ActionSource":0,"Action":"ModifyLoginId","LoginId":"test0007"}
			Logger.comment("获取uid:" + userId + "用户的Lastupdatetime");
			Date oldtime = new YmtUsersWapper().getYmtUsers(Integer.valueOf(userId)).getLastupdatetime();
			lbb.setAction("ModifyLoginId");
			lbb.setUserId(Integer.valueOf(userId));
			lbb.setLoginId("autotest_"+TestDataManager.getRandomNum(100));
			lbb.setActionSource("0");
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals("400", lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(false, lbc.getSuccess(), "验证Success");
			Logger.verifyEquals("LoginNameLengthError", lbc.getString("Result"), "验证Result");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_011(){
		Logger.start(true, "GET查询信息");
		try{
			String mobile=TestDataManager.getPhoneNum();
//			Map user = UserCallService.registerUser(TestDataManager.getEmail(),mobile ,null);
//			int userId = (Integer)user.get("userId");
			String url="http://userservice.ymatou.com/json/reply/LoginBillsRequestDto?Mobile="+mobile+"&Action=12";
			YmatouEasyCall ec=new YmatouEasyCall(url, "GET", "PARAM");
			ec.callService();
			Logger.verifyEquals(ECode.SUCCESS, ec.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", ec.getString("Success"), "验证操作成功");
			Logger.verifyEquals("ok", ec.getString("SuccessMessage"), "验证Msg信息");
			Logger.verifyIsContains("\"Code\":\"Success\"", ec.getString("Result"), "验证Resul信息");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
//	http://userservice.ymatou.com/json/reply/LoginBillsRequestDto?Action=12&UserId=&LoginId=&Mail=&Mobile=13189718709
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_012(){
		Logger.start(true, "GET查询信息,传入部分参数正确返回(修复前会404)");
		try{
			String mobile=TestDataManager.getPhoneNum();
			Map user = UserCallService.registerUser(TestDataManager.getEmail(),mobile ,null);
//			int userId = (Integer)user.get("userId");
			String url="http://userservice.ymatou.com/json/reply/LoginBillsRequestDto?Action=12&UserId=&LoginId=&Mail=&Mobile="+mobile;
			YmatouEasyCall ec=new YmatouEasyCall(url, "GET", "PARAM");
			ec.callService();
			Logger.verifyEquals("400", ec.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", ec.getString("Success"), "验证操作成功");
			Logger.verifyIsContains("\"Code\":\"MobileAlreadyExists\"", ec.getString("Result"), "验证Resul信息");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	//http://userservice.ymatou.com/json/reply/LoginBillsRequestDto?Mobile=jkcklnjn km mn n m&lkd8y9ty78uiuhdjhji&Action=12
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_LoginBillsRequestDto_013(){
		Logger.start(true, "GET查询信息,传入带&字符异常报文不报错");
		try{
			String url="http://userservice.ymatou.com/json/reply/LoginBillsRequestDto?Action=12&Mobile="+FormatUtil.urlEncode("jkcklnjn km mn n m&lkd8y9ty78uiuhdjhji");
			YmatouEasyCall ec=new YmatouEasyCall(url, "GET", "PARAM");
			ec.callService();
			Logger.verifyEquals("200", ec.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", ec.getString("Success"), "验证操作成功");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}